package com.sinder.campus_prophet.utils;

import lombok.Data;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.function.Consumer;

/**
 * 根据入学年份获取个人学期代码
 * 本科四年，一个人只存在八个学期代码(除非重修了，当兵的未知)
 * @author v-xyingde
 * @date 2023/3/24/15:25
 */
public class TermCodeUtils {

    private final static String[] CODE_NAME = new String[] {
            "大一(上)", "大一(下)",
            "大二(上)", "大二(下)",
            "大三(上)", "大三(下)",
            "大四(上)", "大四(下)"
    };

    /**
     * 获取参数学期列表
     * @param y
     * @return
     */
    public static List<YermCode> getTermCode(String y) {
        // 入学年份
        int year = Integer.parseInt(y);
        // 设置"当前学期"
        // 获取当前年和月
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        // 1-8月为年级下学期
        String key = String.valueOf(year).concat("01");
        if (currentMonth > 1 && currentMonth <= 7) {
            key = String.valueOf(currentYear - 1).concat("02");
        } else {
            key = String.valueOf(currentYear).concat("01");
        }
        // 大一上 year01，大一下year02，大二上(year+1)01，大二下(year+1)02，大三上，大三下，大四上，大四下
        List<YermCode> yermCodes = new ArrayList<>();
        int temp = 1;
        for (int i = 0; i < 7; i++) {
            YermCode yermCode = new YermCode();
            if (temp == 1) {
                yermCode.setCode(String.valueOf(year).concat("0").concat(String.valueOf(temp)));
                temp = 2;
            } else {
                yermCode.setCode(String.valueOf(year).concat("0").concat(String.valueOf(temp)));
                year = year + 1;
                temp = 1;
            }
            yermCode.setCodeName(CODE_NAME[i]);
            if (key.equals(yermCode.getCode())) {
                yermCode.setCodeName(CODE_NAME[i] + "(当前学期)");
            }
            yermCodes.add(yermCode);
        }
        return yermCodes;
    }

    /**
     * 获取当前学年代码
     * @param y
     * @return
     */
    public static String getCurrentCode(String y) {
        // 入学年份
        int year = Integer.parseInt(y);
        // 设置"当前学期"
        // 获取当前年和月
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        // 1-8月为年级下学期
        String key;
        if (currentMonth > 1 && currentMonth <= 7) {
            key = String.valueOf(currentYear - 1).concat("02");
        } else {
            key = String.valueOf(currentYear).concat("01");
        }
        return key;
    }

    @Data
    public static class YermCode {
        private String code;
        private String codeName;
    }
}
